<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/>
    <title>详细页</title>
    <link rel="stylesheet" href="/bus/js/css/layui.css" media="all">
    <style>
        body {
            margin: 10px;
        }

        .layui-textarea {
            min-height: 555px;
            height: auto;
            line-height: 20px;
            padding: 6px 10px;
            resize: vertical;
        }
    </style>
</head>
<body>

<forms class="layui-form">


    <!--    <textarea id="content" style="display: none;"  th:text="${tc.content}"></textarea>-->

    <!--    <pre th:text="${tc.content}" class="layui-code-view"  lay-title="JavaScript" lay-skin="notepad" >//在里面存放任意的文本内容-->
    <!--             </pre>-->

    <div class="layui-row">

        <div class="layui-col-xs12 layui-col-md10">
            <div class="grid-demo grid-demo-bg1">
                <div class="layui-input-block123">
            <textarea placeholder="请输入内容" id="content" class="layui-textarea" th:text="${tc.content}"
                      th:tdid="${tc.id}"></textarea>
                </div>

            </div>
        </div>

        <div class="layui-col-xs6 layui-col-md2">
            <div class="grid-demo">
                <legend>代码块</legend>
                <div id="test13" class="demo-tree-more"></div>

            </div>
        </div>

    </div>

    <!--    <div class="layui-form-item layui-form-text">-->

    <!--        <label class="layui-form-label">-->
    <!--        </label>-->
    <!--    </div>-->


    <div class=" layui-form-item">
        <div class="layui-input-block123">
            <button class="layui-btn" lay-filter="saveSubmitBtn" id="saveSubmitBtn" hidden>保存
            </button>
        </div>
    </div>


</forms>
<script src="/bus/js/layui.js"></script>
<script src="/bus/common.js"></script>

<script>
    var tree = layui.tree
    //模拟数据1
    var data1;

    function addContent(myval) {
        var obj = $("#content").get(0);
        var str = myval;
        if (document.selection) {
            obj.focus();
            var sel = document.selection.createRange();
            sel.text = str;
        } else if (typeof obj.selectionStart === 'number' && typeof obj.selectionEnd === 'number') {
            var startPos = obj.selectionStart;
            var endPos = obj.selectionEnd;
            var tmpStr = obj.value;
            obj.value = tmpStr.substring(0, startPos) + str + tmpStr.substring(endPos, tmpStr.length);
        } else {
            obj.value += str;
        }
    }

    $(".addcontent").click(function () {
        var obj = $("#content").get(0);
        var str = $(this).attr("myva");
        if (document.selection) {
            obj.focus();
            var sel = document.selection.createRange();
            sel.text = str;
        } else if (typeof obj.selectionStart === 'number' && typeof obj.selectionEnd === 'number') {
            var startPos = obj.selectionStart;
            var endPos = obj.selectionEnd;
            var tmpStr = obj.value;
            obj.value = tmpStr.substring(0, startPos) + str + tmpStr.substring(endPos, tmpStr.length);
        } else {
            obj.value += str;
        }
    });

    var mydefine={
        "title": "自定义",
        "text": "",
        "children": [
            {
                "title": "循环:\n #foreach ($column in $columns)\n 内容 \n #end",
                "text": "#foreach ($column in $columns)\n内容\n#end"
            }
        ]
    };


    $(function () {
        getQuickCode();
        $("#content").height((document.body.clientHeight - 50) + "px");
    })

    $("#saveSubmitBtn").click(function () {
        var content = $("#content").val();
        var tdid = $("#content").attr("tdid");
        var datax = "{\"id\":" + tdid + ",\"content\"" + ":\"" + content + "\"}";
        datax = {
            "id": tdid
            , "content": content
        }

        commonUtil.sendRequest("PUT", datax, "/template/update/");
    })

    var myQuickCode=new Array();
    //自定义模板语句
    function getQuickCode() {
        var datax = {
            "dictLabel": '',
            "dictType": 'quick_code'
        }
        var urlx = "/dict/getDictData";
        var func = function (data) {
            for(var code of data.data){
                var va1={
                    "title": code.dictLabel,
                    "text": code.dictValue
                }
                myQuickCode.push(va1);
            }
            mydefine.children=myQuickCode;
            $.getJSON("/data/java.json", function (data) {
                data1 = data.data;
                $.each(data, function (haha, info) {
                })
                data1[4]=mydefine;
                //无连接线风格
                tree.render({
                    elem: '#test13'
                    , data: data1
                    , showLine: false  //是否开启连接线
                    , click: function (obj) {
                        var data = obj.data;
                        if (data.children == undefined) {
                            addContent(data.text);
                        }
                    }
                });
            })

            layer.msg("操作成功");
        }
        commonUtil.sendRequest("POST", datax, urlx, func);
    }

</script>

</body>
</html>
